1   /**
2    * Copyright 2014 Netflix, Inc.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package rx.internal.operators;
17  
18  import static org.mockito.Matchers.any;
19  import static org.mockito.Matchers.anyInt;
20  import static org.mockito.Mockito.inOrder;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.never;
23  import static org.mockito.Mockito.times;
24  import static org.mockito.Mockito.verify;
25  
26  import org.junit.Test;
27  import org.mockito.InOrder;
28  
29  import rx.Observable;
30  import rx.Observer;
31  import rx.functions.Func1;
32  
33  public class OperatorSkipWhileTest {
34  
35      @SuppressWarnings("unchecked")
36      Observer<Integer> w = mock(Observer.class);
37  
38      private static final Func1<Integer, Boolean> LESS_THAN_FIVE = new Func1<Integer, Boolean>() {
39          @Override
40          public Boolean call(Integer v) {
41              if (v == 42)
42                  throw new RuntimeException("that's not the answer to everything!");
43              return v < 5;
44          }
45      };
46  
47      private static final Func1<Integer, Boolean> INDEX_LESS_THAN_THREE = new Func1<Integer, Boolean>() {
48          int index = 0;
49          @Override
50          public Boolean call(Integer value) {
51              return index++ < 3;
52          }
53      };
54  
55      @Test
56      public void testSkipWithIndex() {
57          Observable<Integer> src = Observable.just(1, 2, 3, 4, 5);
58          src.skipWhile(INDEX_LESS_THAN_THREE).subscribe(w);
59  
60          InOrder inOrder = inOrder(w);
61          inOrder.verify(w, times(1)).onNext(4);
62          inOrder.verify(w, times(1)).onNext(5);
63          inOrder.verify(w, times(1)).onCompleted();
64          inOrder.verify(w, never()).onError(any(Throwable.class));
65      }
66  
67      @Test
68      public void testSkipEmpty() {
69          Observable<Integer> src = Observable.empty();
70          src.skipWhile(LESS_THAN_FIVE).subscribe(w);
71          verify(w, never()).onNext(anyInt());
72          verify(w, never()).onError(any(Throwable.class));
73          verify(w, times(1)).onCompleted();
74      }
75  
76      @Test
77      public void testSkipEverything() {
78          Observable<Integer> src = Observable.just(1, 2, 3, 4, 3, 2, 1);
79          src.skipWhile(LESS_THAN_FIVE).subscribe(w);
80          verify(w, never()).onNext(anyInt());
81          verify(w, never()).onError(any(Throwable.class));
82          verify(w, times(1)).onCompleted();
83      }
84  
85      @Test
86      public void testSkipNothing() {
87          Observable<Integer> src = Observable.just(5, 3, 1);
88          src.skipWhile(LESS_THAN_FIVE).subscribe(w);
89  
90          InOrder inOrder = inOrder(w);
91          inOrder.verify(w, times(1)).onNext(5);
92          inOrder.verify(w, times(1)).onNext(3);
93          inOrder.verify(w, times(1)).onNext(1);
94          inOrder.verify(w, times(1)).onCompleted();
95          inOrder.verify(w, never()).onError(any(Throwable.class));
96      }
97  
98      @Test
99      public void testSkipSome() {
100         Observable<Integer> src = Observable.just(1, 2, 3, 4, 5, 3, 1, 5);
101         src.skipWhile(LESS_THAN_FIVE).subscribe(w);
102 
103         InOrder inOrder = inOrder(w);
104         inOrder.verify(w, times(1)).onNext(5);
105         inOrder.verify(w, times(1)).onNext(3);
106         inOrder.verify(w, times(1)).onNext(1);
107         inOrder.verify(w, times(1)).onNext(5);
108         inOrder.verify(w, times(1)).onCompleted();
109         inOrder.verify(w, never()).onError(any(Throwable.class));
110     }
111 
112     @Test
113     public void testSkipError() {
114         Observable<Integer> src = Observable.just(1, 2, 42, 5, 3, 1);
115         src.skipWhile(LESS_THAN_FIVE).subscribe(w);
116 
117         InOrder inOrder = inOrder(w);
118         inOrder.verify(w, never()).onNext(anyInt());
119         inOrder.verify(w, never()).onCompleted();
120         inOrder.verify(w, times(1)).onError(any(RuntimeException.class));
121     }
122     
123     @Test
124     public void testSkipManySubscribers() {
125         Observable<Integer> src = Observable.range(1, 10).skipWhile(LESS_THAN_FIVE);
126         int n = 5;
127         for (int i = 0; i < n; i++) {
128             @SuppressWarnings("unchecked")
129             Observer<Object> o = mock(Observer.class);
130             InOrder inOrder = inOrder(o);
131             
132             src.subscribe(o);
133             
134             for (int j = 5; j < 10; j++) {
135                 inOrder.verify(o).onNext(j);
136             } 
137             inOrder.verify(o).onCompleted();
138             verify(o, never()).onError(any(Throwable.class));
139         }
140     }
141 }